package com.longrise.co.ui.carlife.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.longrise.co.R;
import com.longrise.co.api.ApiConstants;
import com.longrise.co.ui.carlife.adapter.IllegalInquiriesAdapter;
import com.longrise.co.ui.carlife.contract.CarLifeContract;
import com.longrise.co.ui.carlife.model.CarLifeModel;
import com.longrise.co.ui.carlife.model.IllegalInquiriesBean;
import com.longrise.co.ui.carlife.presenter.CarLifePresenter;
import com.longrise.co.utils.Constants;
import com.ly.common.base.BaseFragment;
import com.ly.common.commonutils.StringUtils;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by luoyang on 2017-7-24.
 */

public class IllegalInquiriesFragment extends BaseFragment<CarLifePresenter, CarLifeModel> implements CarLifeContract.View<IllegalInquiriesBean>, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnRecyclerViewItemClickListener {

    @Bind(R.id.head_title)
    TextView headTitle;
    @Bind(R.id.car_no)
    TextView carNo;
    @Bind(R.id.car_illegal)
    TextView carIllegal;
    @Bind(R.id.car_deduction_points)
    TextView carDeductionPoints;
    @Bind(R.id.search_no_data_tv)
    TextView searchNoDataTv;
    @Bind(R.id.no_data_tip)
    RelativeLayout noDataTip;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;

    private IllegalInquiriesAdapter mAdapter;

    private String mCarNo, mCarVin, mEngineNumber;

    private IllegalInquiriesBean mIllegalInquiriesBean;

    @Override
    protected int getLayoutResource() {
        return R.layout.fg_illegal_inquiries;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    protected void initView() {

        mCarNo = getArguments().getString("carNo");
        mCarVin = getArguments().getString("carVin");
        mEngineNumber = getArguments().getString("engineNumber");

        setText(headTitle, "违章查询");
        setText(searchNoDataTv, "恭喜您，没有违章记录");
        setText(carNo, mCarNo);
        initRecyclerView();
        onRefresh();
    }

    private void initRecyclerView() {
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setColorSchemeColors(Constants.RECYCLER_RGB);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(layoutManager);
        mAdapter = new IllegalInquiriesAdapter(null);

        mAdapter.openLoadMore(ApiConstants.PAGE_SIZE_10, true);
        mAdapter.setOnRecyclerViewItemClickListener(this);
        recyclerView.setAdapter(mAdapter);
    }


    @Override
    public void requestSuccess(IllegalInquiriesBean illegalInquiriesBean) {
        mIllegalInquiriesBean = illegalInquiriesBean;
        int count = 0;
        String sumMoney = "0";
        String sumDegree = "0";
        if(!StringUtils.isEmpty(illegalInquiriesBean.getSummoney())) {
            sumMoney = illegalInquiriesBean.getSummoney();
        }

        if(!StringUtils.isEmpty(illegalInquiriesBean.getSumdegree())) {
            sumDegree = illegalInquiriesBean.getSumdegree();
        }

        if (illegalInquiriesBean != null && illegalInquiriesBean.getList() != null && illegalInquiriesBean.getList().size() > 0) {
            count = illegalInquiriesBean.getList().size();
            mAdapter.setNewData(illegalInquiriesBean.getList());
            mAdapter.notifyDataChangedAfterLoadMore(false);
        } else {
            //没有搜索到结果
            noDataTip.setVisibility(View.VISIBLE);
            mAdapter.notifyDataChangedAfterLoadMore(false);
        }
            setText(carIllegal, "违法：" + String.valueOf(count) + "条");
            setText(carDeductionPoints, "扣分总计：" + sumDegree + "分    罚款总计：" + sumMoney + "元");
    }

    @Override
    public void onComplete() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }
        //当走onError方法的时候，这里对定义的全局bean进行判断，来显示没有违章记录的图片！
        if(mIllegalInquiriesBean==null){
            noDataTip.setVisibility(View.VISIBLE);
            mAdapter.notifyDataChangedAfterLoadMore(false);
        }
    }

    @OnClick(R.id.head_back)
    public void onViewClicked() {
        _mActivity.onBackPressed();
    }

    public static IllegalInquiriesFragment newInstance(String carNo, String carVin, String engineNumber) {

        Bundle args = new Bundle();
        args.putString("carNo", carNo);
        args.putString("carVin", carVin);
        args.putString("engineNumber", engineNumber);
        IllegalInquiriesFragment fragment = new IllegalInquiriesFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onRefresh() {
        noDataTip.setVisibility(View.GONE);
        mPresenter.getIllegalInquiries(mCarNo, mCarVin,mEngineNumber);
    }

    @Override
    public void onItemClick(View view, int i) {

    }
}
